package br.diego.tb.dataconversion;


public class CaesarShift 
{
	private static final int ALPHABET_OFFSET = 25;

	public static void main(String[] args)
	{
		caesar_shift("CAT");
		caesar_shift("cat");
		caesar_shift("ZzAT");
		
		caesar_shift("HELLO SECRET AGENTZ");
	}
	
	public static String caesar_shift(String s)
	{
		char[] unencryptedChars = s.toCharArray();
		
		int lengthOfInput = unencryptedChars.length;
		
		char[] encryptedChars = new char[lengthOfInput];
		
		for(int i = 0; i < lengthOfInput; i ++)
		{
			char actual = unencryptedChars[i]; 
			
			encryptedChars[i] = isAlpha(actual) ? retrieveNextChar(unencryptedChars[i]) : actual; 
		}
		
		System.out.println(encryptedChars);
		
		return new String(encryptedChars);
	}

	private static char retrieveNextChar(char actual)
	{
		char nextChar;
		
		if(actual == 'z' || actual == 'Z')
		{
			nextChar = (char) ((int) actual - ALPHABET_OFFSET);
		}
		else
		{
			nextChar = (char) ((int) actual + 1);
		}
		
		return nextChar;
		
	}
	
	private static boolean isAlpha(char candidate)
	{
		
		boolean isAlpha = (candidate >= 'A' && candidate <= 'z');

		return isAlpha;
	}
}
